package com.ssy.lingxi.sms.controller;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.sms.api.common.ServiceTypeEnum;
import com.ssy.lingxi.sms.api.entity.SmsSendMsg;
import com.ssy.lingxi.sms.api.model.SmsVO;
import com.ssy.lingxi.sms.entity.SmsConfig;
import com.ssy.lingxi.sms.service.IAliService;
import com.ssy.lingxi.sms.service.ISmsConfigService;
import com.ssy.lingxi.sms.service.ITencentService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu: 短信服务
 * 短信发送入口类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/10
 */
@RestController
@RequestMapping("/sms")
public class SendSmsController {

    @Resource
    private ISmsConfigService smsConfigService;

    @Resource
    private ITencentService tencentService;

    @Resource
    private IAliService aliService;

    /**
     * 发送短信
     * @param smsVO 参数实体
     * @return 短信发送结果
     */
    @RequestMapping("/sendSms")
    public Wrapper<List<SmsSendMsg>> sendSms(@RequestBody @Valid SmsVO smsVO) {
        //查询生效的服务器配置
        SmsConfig smsConfig = smsConfigService.findEnableSmsConfig();
        if(smsConfig != null){
            Integer serviceType = smsConfig.getServiceType();
            if(serviceType.equals(ServiceTypeEnum.Tencent.getCode())){
                return tencentService.sendSms(smsVO,smsConfig);
            }else if(serviceType.equals(ServiceTypeEnum.Ali.getCode())){
                return aliService.sendSms(smsVO,smsConfig);
            }
        }
        return Wrapper.fail(ResponseCode.SMS_CONFIG_NOT_EXIST);
    }

}
